<script setup>
import { artGetChannelsService, artDeleteChannelService } from '@/api/article'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { ElMessage } from 'element-plus'

// 加载效果 请求前打开,请求后关闭
const loading = ref(false)
const channelList = ref([])
const dialog = ref()
// 调用 api 获取分类列表内容
const getChanelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  // console.log(res)
  channelList.value = res.data.data
  loading.value = false
}
getChanelList()
// 点击编辑
const onEditChannel = (row) => {
  // console.log(row)
  dialog.value.open(row)
}
// 点击删除
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认要删除吗?', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDeleteChannelService(row.id)
  // console.log(row)
  ElMessage.success('编辑成功')
  getChanelList()
}
// 点击添加分类
const onAddChannel = () => {
  // dialogVisible.value = true
  dialog.value.open({})
}
// 一点确认就重新渲染页面
const onSuccess = () => {
  getChanelList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <!-- el 表格 :data需要的数据 lable是列名，pros是填充数据，type=“index” 有索引号-->
    <!-- v-loading="loading" 加载效果 -->
    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="100">
        <!-- 自定义列 -->
        <!-- #default 后面是 scope, scope里面有 row column,$index等信息, 这里解构出来了 -->
        <!-- row 就是 channelList 一行的信息 index是下标-->
        <template #default="{ row }">
          <!-- plain 是镂空 ,circle是圆形, icon是图标-->
          <el-button
            :icon="Edit"
            type="primary"
            plain
            circle
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            type="danger"
            plain
            circle
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹层 -->
    <Channel-edit ref="dialog" @success="onSuccess"></Channel-edit>
  </page-container>
</template>
